package cn.com.checne.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.dao.CultureCategoryDao;
import cn.com.checne.dao.CultureInfoDao;
import cn.com.checne.model.domain.CultureCategory;
import cn.com.checne.model.domain.CultureInfo;
import cn.com.checne.model.dto.CultureQuery;
import cn.com.checne.util.HeaderUtils;
import cn.com.checne.util.LanguageUtils;
import cn.com.checne.util.PageBean;

@Controller
public class CultureFrontController {
	protected static final Logger logger = Logger.getLogger(CultureFrontController.class);

	@Resource
	private CultureCategoryDao cateDao;
	@Resource
	private CultureInfoDao cultureDao;


	//首页面
	@RequestMapping(value="/{lg}/culture",method=RequestMethod.GET)
	public ModelAndView index(
			@PathVariable String lg,
			@RequestParam(value = "cateId", required = false, defaultValue = "0") String cId,
			@RequestParam(value = "curPage", required = false, defaultValue = "1") String pg,
			HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		lg = StringUtils.isBlank(lg) ? Constants.LANGUAGE_CN : lg ;
		String viewName = "front/" + lg + "/culture-list" ;
		int languageId = LanguageUtils.getLanguage(lg);
		int cateId = Integer.parseInt(cId);
		int curPage = Integer.parseInt(pg);
		List<CultureCategory> cateList = cateDao.getAll(languageId);
		CultureCategory cate = new CultureCategory() ;
		if(cateId == 0){
			if(cateList != null && cateList.size() > 0){
				cate = cateList.get(0);
			}
		}else{
			cate = cateDao.getCultureCategory(cateId);
		}

		CultureQuery query = new CultureQuery();
		query.setLanguageId(languageId);
		query.setCateId(cate.getCateId());
		query.setIsShowList(1);
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<CultureInfo> cultureList = cultureDao.getCultureListByCondition(query, pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(cultureDao.getCultureCountByCondition(query));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		CultureInfo cultureInfo = new CultureInfo();
		if(cate.getShowType() == 0 ){ //直接显示详情
			cultureInfo = (cultureList != null && cultureList.size() > 0 ) ? cultureList.get(0) : cultureInfo ;
		}
	    
		modelMap.put("cateList", cateList);
		modelMap.put("cultureList", cultureList);
		modelMap.put("culture", cultureInfo);
		modelMap.put("cate", cate);
		modelMap.put("pageBean", pageBean);
		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//详情页面
	@RequestMapping(value="/{lg}/culture/{cultureId}",method=RequestMethod.GET)
	public ModelAndView detail(
			@PathVariable String lg,
			@PathVariable int cultureId,
			HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		String viewName = "front/" + lg + "/culture-detail" ;
		
		int languageId = LanguageUtils.getLanguage(lg);
		List<CultureCategory> cateList = cateDao.getAll(languageId);
		CultureInfo culture = cultureDao.getCultureById(cultureId);
		
		modelMap.put("cateList", cateList);
		modelMap.put("culture", culture);
		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
}